package com.yanchangapp.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yanchangapp.R;
import com.zxl.baselib.util.ui.ResHelper;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.util.window.KeyboardHelper;

/**
 * Created by apple on 2019-08-13.
 * description:购物车数量加减view
 */
public class AmountView extends LinearLayout {
    // 当前购买的数量...
    private int amount = 1;
    // 默认为最大库存即可
    private int mGoodsStorage = Integer.MAX_VALUE;

    // private int mGoodsStorage = Integer.MAX_VALUE;

    private Button mBtnMinus,mBtnAdd;

    private TextView tvTextNum;

    private AmountChangeListener mAmountChangeListener = null;

    private Dialog mAmountDialog = null;

    private TextView tvCurrentNumDes;

    private EditText etNum;

    private Context mContext;

    public void setOnAmountChangeListener(AmountChangeListener amountChangeListener){
        this.mAmountChangeListener = amountChangeListener;
    }

    public void setGoodsStorage(int num){
        this.mGoodsStorage = num;
    }

    public AmountView(Context context) {
        this(context,null);
    }

    public AmountView(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public AmountView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initViews(context);
    }

    private void initViews(Context context) {
        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.view_amount_view,this);
        mBtnMinus = findViewById(R.id.btnMinus);
        mBtnAdd = findViewById(R.id.btnPlus);
        tvTextNum = findViewById(R.id.tvNum);
        initListener();
    }

    public void setNowShowText(int showAmount){
        this.amount = showAmount;
        tvTextNum.setText(String.valueOf(showAmount));
    }

    private void initListener() {
        mBtnMinus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (amount > 1){
                    amount --;
                    tvTextNum.setText(String.valueOf(amount));
                    if (mAmountChangeListener != null){
                        mAmountChangeListener.onAmountChanged(amount);
                    }
                }

            }
        });

        mBtnAdd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                 if (amount < mGoodsStorage){
                     amount ++;
                     tvTextNum.setText(String.valueOf(amount));

                     if (mAmountChangeListener != null){
                         mAmountChangeListener.onAmountChanged(amount);
                     }
                 }
            }
        });

        tvTextNum.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // showDeleteDialog(mContext);
                showNumChanged(mContext);
            }
        });
    }

    private void showDeleteDialog(Context context) {
        if (mAmountDialog == null){
            View view  = LayoutInflater.from(context).inflate(R.layout.dialog_set_num, null);
            mAmountDialog = new InputDialog(context, R.style.MyDialog);
            TextView mTvCancel =  view.findViewById(R.id.tvCancel);
            TextView mTvEnsure = view.findViewById(R.id.tvEnsure);
            etNum = view.findViewById(R.id.etNum);
            tvCurrentNumDes = view.findViewById(R.id.tvCurrentNum);
            Window window = mAmountDialog.getWindow();
            window.setGravity(Gravity.CENTER);
            window.setWindowAnimations(R.style.BottomDialog);
            window.getDecorView().setPadding(56, 0, 56, 0);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
            mAmountDialog.setContentView(view);
            mTvCancel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mAmountDialog.dismiss();
                }
            });

            mTvEnsure.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    String strNum = etNum.getText().toString();
                    long num = Long.parseLong(strNum);
                    if (num> mGoodsStorage){
                        tvCurrentNumDes.setText("最大库存:"+mGoodsStorage);
                        tvCurrentNumDes.setVisibility(VISIBLE);
                    }else {
                        if (strNum.length() > 9){
                            UIUtils.showToast("数量不能超过999999999");
                            return;
                        }
                        amount = Integer.parseInt(strNum);
                        tvTextNum.setText(String.valueOf(amount));
                        if (mAmountChangeListener != null){
                            mAmountChangeListener.onAmountChanged(amount);
                        }
                        mAmountDialog.dismiss();
                    }
                }
            });
        }
        etNum.setText(String.valueOf(amount));
        etNum.setSelection(String.valueOf(amount).length());
        tvCurrentNumDes.setVisibility(GONE);
        mAmountDialog.show();
    }

    private void showNumChanged(Context context){
        new GoodsNumDialog.Builder(context)
                .setCurrentNum(amount)
                .setGoodsStorage(mGoodsStorage)
                .setGoodsEnsureListener(new GoodsNumDialog.GoodsEnsureClickListener() {
                    @Override
                    public void onEnsureClickSuccess(int num) {
                        amount = num;
                        tvTextNum.setText(String.valueOf(amount));
                        if (mAmountChangeListener != null){
                            mAmountChangeListener.onAmountChanged(amount);
                        }
                    }
                }).build().show();
    }

    public interface AmountChangeListener{
        void onAmountChanged(int count);
    }
}
